
#import "LBPathFinder.h"

@implementation LBPathFinder

@synthesize m_parthPoints;
@synthesize m_endPosition;

+(CGPoint)translatePositionToCenterPointFromRow:(int)row column:(int)column
{
    return CGPointMake(COORDINATE_START_X+(column-1+0.5)*BALL_WIDTH+CENTER_POINT_X_CORRECT, COORDINATE_START_Y+(row-1+0.5)*BALL_HEIGHT+CENTER_POINT_Y_CORRECT);
}

// 不仅仅是centerPoint，而是任意touchPoint到行、列号的映射
// 能够将任一给定像素点映射到某一个地图区块中
+(BOOL)tranllateTouchPointToPositionFromPoint:(CGPoint)touchPoint toRow:(int *)pRow toColumn:(int *)pColumn
{
    for(int row=1; row<=MAX_ROW; row++ )
    {
        for(int column=1; column<=MAX_CANON; column++)
        {
            CGPoint centerPoint = [LBPathFinder translatePositionToCenterPointFromRow:row column:column];
            
            if (centerPoint.x - BALL_WIDTH/2 < touchPoint.x
                && centerPoint.x + BALL_WIDTH/2 > touchPoint.x
                && centerPoint.y - BALL_HEIGHT/2 < touchPoint.y
                && centerPoint.y + BALL_HEIGHT/2 > touchPoint.y)
            {
                *pRow = row;
                *pColumn = column;
                return TRUE;
            }
        }
    }

    return FALSE;
}

-(id)init
{
    if( !(self = [super init]))
    {
        return nil;
    }
    
    for (int rowTmp=1; rowTmp<=MAX_ROW; rowTmp++)
    {
        for (int columnTmp=1; columnTmp<=MAX_COLUMN; columnTmp++)
        {
            m_sourceMap[rowTmp][columnTmp] = POSITION_EMPTY;
        }
    }
    
    if (!m_startPosition)
    {
        m_startPosition = [[LBPosition alloc] initWithRow:-1 column:-1];
    }
    
    if(!m_endPosition)
    {
        m_endPosition = [[LBPosition alloc] initWithRow:-1 column:-1];
    }
    
    if(!m_parthPoints)
    {
        m_parthPoints = [[NSMutableArray alloc] init];
    }
    
    if(!m_positionsArray)
    {
        m_positionsArray = [[NSMutableArray alloc] init];
    }
    
    return self;
}

-(BOOL)findPathFromStartPosition:(LBPosition*)startPosition toEndPosition:(LBPosition*)endPositon;
{    
    // clear path array
    [m_parthPoints removeAllObjects];
    
    m_endPosition = endPositon;
    
    int result = 0;
    
    result = [self FindPath:startPosition.m_column-1 yStart:startPosition.m_row-1 xTarget:endPositon.m_column-1 yTarget:endPositon.m_row-1];
    
    if( found == result )
    {        
        if( nil != pathStorage )
        {            
            for (int count = 0; count < pathLength; count++) 
            {
                LBPosition  *nextPosition = [LBPosition alloc];
                
                nextPosition.m_column = pathStorage[count*2]+1;
                nextPosition.m_row = pathStorage[count*2+1]+1;
                
                [m_parthPoints addObject:nextPosition];
            }
        }
        else
        {
            NSLog(@"pathStorage is nil!!!");
        }
        
        return TRUE;
    }
    
    return FALSE;
}


-(BOOL)setPositionStateToMapWithType:(enum MAP_TYPE)mapType atPosition:(LBPosition *)position toState:(enum MAP_POSITION_STATE)positionState
{
    if (MAP_SOURCE == mapType)
    {
        switch (positionState) 
        {
            case POSITION_EMPTY:
                [self setWalkabilityCoordX:position.m_column-1 CoordY:position.m_row-1 walkBility:walkable];
                break;
                
            case POSITION_BALL:                    
                [self setWalkabilityCoordX:position.m_column-1 CoordY:position.m_row-1 walkBility:unwalkable];
                break;
                
            default:
                break;
        }
    }
            
    return TRUE;
}

-(NSMutableArray *)getPositionsWithNum:(int)num withState:(enum MAP_POSITION_STATE)positionState
{
    [m_positionsArray removeAllObjects];
    
    int *positionsIntArray = [self generateRandomObjectsNumber:num Width:1 Height:1];
    
    if( positionsIntArray != nil )
    {
        for (int i = 0; i<num; i++) 
        {
            LBPosition  *position = [[LBPosition alloc] initWithRow:positionsIntArray[i*2+1]+1 column:positionsIntArray[i*2]+1];
            [m_positionsArray addObject:position];
        }
    }
    else
    {
        // 尝试获取个数为 1
        positionsIntArray = [self generateRandomObjectsNumber:1 Width:1 Height:1];
        
        if( positionsIntArray != nil )
        {
            LBPosition  *position = [[LBPosition alloc] initWithRow:positionsIntArray[1]+1 column:positionsIntArray[0]+1];
            [m_positionsArray addObject:position];            
        }
        else
        {
            // 1个都获取不到，输了
            // 再强制设置为0，外部可通过数组为0认为输了
            [m_positionsArray removeAllObjects];
        }
    }
    
    NSLog(@"array length: %d, num:%d, File:%s, Line:%d",[m_positionsArray count],num,__FILE__,__LINE__);
    
    return m_positionsArray;
}

@end
